<?php

define('IN_ECS', true);

$act = !empty($_REQUEST['act']) ? $_REQUEST['act'] : '';

require(dirname(__FILE__) . '/includes/init.php');

if ($act == 'get_api_storelist'){
	$data = $db->getAll("SELECT id,name FROM " . $ecs->table('store'));
}

elseif($act == 'edit_api_code'){
	$sql = "UPDATE ".$ecs->table('code'). " SET code_fee = '".$_REQUEST[code_val]."' WHERE code_name = '".$_REQUEST[code_name]."' AND type_id = '".$_REQUEST[type_id]."'";
	if( $db->query($sql) ){
		$data = 1;
	}else{
		$data = 0;
	}
}

elseif($act == 'set_api_code'){
	$sql = "INSERT INTO ".$ecs->table('code'). " (code_name,code_fee,type_id) VALUES ('$_REQUEST[code_name]', '$_REQUEST[code_fee]', '$_REQUEST[type_id]')";
	if( $db->query($sql) ){
		$data = $db->insert_id();
	}else{
		$data = 0;
	}
}

elseif($act == 'get_api_code'){
	$where = ' WHERE 1 ';
	if($_REQUEST['type_id']) $where .= " AND type_id = '".$_REQUEST['type_id']."'";
	if($_REQUEST['code_id']){
		$where .= " AND code_id = '".$_REQUEST['code_id']."'";
	}else{
		$where .= " AND code_name = '".$_REQUEST['code_name']."'";
	}
	$data = $db->getRow("SELECT * FROM " . $ecs->table('code') . $where);
}

elseif($act == 'get_api_region'){
	$data = $db->getAll("SELECT * FROM " . $ecs->table('region') . " WHERE parent_id = '1'");
}

elseif($act == 'get_api_area'){
	$data = $db->getAll("SELECT * FROM " . $ecs->table('region') . " WHERE parent_id = '".$_REQUEST['id']."'");
}

elseif($act == 'get_api_store'){
	$data = $db->getAll("SELECT id,name FROM " . $ecs->table('store') . " WHERE id_dist = '".$_REQUEST["id_dist"]."'");
}

elseif($act == 'get_region_data'){
	$data = $db->getRow("SELECT * FROM " . $ecs->table('region') . " WHERE region_id = '".$_REQUEST["id"]."'");
}

elseif($act == 'get_order_data'){
	include_once(ROOT_PATH . 'includes/lib_order.php');
	$data = order_info($_REQUEST["id"]);
}

die(json_encode($data));

?>